public class Solution108 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(0,nums.length-1,nums);
    }

    private TreeNode dfs(int start, int end, int[] nums) {
        if (start>end){
            return null;
        }
        int mid=(start+end)/2;
        TreeNode treeNode=new TreeNode(nums[mid]);
        treeNode.left=dfs(start,mid-1,nums);
        treeNode.right=dfs(mid+1,end,nums);
        return treeNode;
    }

    public static void main(String[] args) {
        System.out.println(new Solution108().sortedArrayToBST(new int[]{-10,-3,0,5,9}).val);
    }
}
